/**
 * @description 路由
 * @author 张航
 * @date 2020-04-27 15:17:01
 * @version V1.0.0
 */

/**
 * @functionName importPage
 * @param {String} name 页面名称
 * @return {Object} 返回页面对象
 * @description 根据名称导入相应的页面
 * @author 张航
 * @date 2020-04-27 15:17:30
 * @version V1.0.0
 */
const importPage = (mode, name) => {
  if (mode === 'main') {
    return import('@/layout/Home')
  }
  return import(`@/views/${mode}/${name}`)
}

/**
 * @functionName routerEach
 * @param {Object} routerMap 后台返回的路由
 * @return {Object} 返回导入页面之后的路由
 * @description 将后台返回的路由中的页面名称处理成导入页面的形式
 * @author 张航
 * @date 2020-04-27 15:29:38
 * @version V1.0.0
 */
export const routerEach = routerMap => {
  const accessedRouters = []
  routerMap.map(route => {
    const {
      strUrl,
      strModPK,
      strName,
      children,
      strIco,
      isPopWin,
      strAuthorityGuids
    } = route

    const strPath = `/${strModPK === 'main' ? '' : strUrl}`
    const itemObj = {
      path: strPath,
      name: strUrl,
      component: importPage(strModPK, strUrl),
      children,
      meta: {
        path: strPath,
        title: strName,
        icon: strIco,
        show: isPopWin === 1,
        auth: strAuthorityGuids
      }
    }

    if (itemObj.children && itemObj.children.length) {
      itemObj.children = routerEach(itemObj.children)
    } else {
      delete itemObj.children
    }

    accessedRouters.push(itemObj)
  })
  return accessedRouters
}

/**
 * @functionName getMenuByRouter
 * @param {Array} router 路由
 * @return {Array} 返回菜单
 * @description 根据路由生成菜单列表
 * @author 张航
 * @date 2020-04-28 14:01:08
 * @version V1.0.0
 */
export const getMenuByRouter = router => {
  const res = []
  router.map(item => {
    const { path = '', title = '', icon = '', show = true } = item.meta
    if (show) {
      const obj = {
        path,
        title,
        icon
      }
      if (item.children && item.children.length) {
        obj.children = getMenuByRouter(item.children)
      }
      res.push(obj)
    }
  })
  return res
}

